﻿MPT.addAction("friend_find", function(_elm) {

    var method_jQs = $('.e_method', _elm);
    var result_list_jQ = $('.e_result_list', _elm);
    var nick_input_jQ = $('.e_nick_input', _elm);
    var nick_submit_jQ = $('.e_nick_submit', _elm);
    var result_count_jQ = $('.e_result_count', _elm);
    var result_title_jQ = $('.e_result_title', _elm);
    var gamearea_jQ = $('.e_gamearea', _elm);
    var gamefwq_jQ = $('.e_gamefwq', _elm);
    var province_jQ = $('.e_province', _elm);
    var page_jQ = $('.e_page', _elm);
	var title_jQs = $('.e_title', _elm);
    var area_list, server_list, province_list;

    var addActionResult = function(_res_elm, _data) {
        var add_jQ = $('.e_add', _res_elm);
        var delete_jQ = $('.e_delete', _res_elm);
        var follow_jQ = $('.e_follow', _res_elm);
        var cancel_jQ = $('.e_cancel', _res_elm);
        var nick_name = _data.nickName;
        var uid = _data.uid;

        //todo:待删
        if (!nick_name) nick_name = $.parseJSON(_data).NICKNAME;
        if (!uid) uid = $.parseJSON(_data).USERID;

        //添加好友
        add_jQ.click(function() {
            var data = { "uid": uid, "nickname": nick_name };
            SocialUtil.displayAddDlg(data, function() {
                add_jQ.css({ "filter": "gray", "-moz-opacity": 0.6, "opacity": 0.6, "cursor": "default" }).unbind('click');
            });
        });

        //删除好友
        delete_jQ.click(function() {
            var data = { "uid": uid, "nickName": nick_name };
            SocialUtil.displayDelDlg(data, function() {
                Tip.show("删除好友成功");
                delete_jQ.addClass('hide');
                add_jQ.removeClass('hide');
            });
        });

        //添加关注
        follow_jQ.click(function() {
            var data = { "targetID": uid, "gids": 0 };
            Backend.follow(data, function(_resp) {
                if (Backend.resultValidate(_resp)) {
                    Tip.show("添加关注成功");
                    follow_jQ.addClass('hide');
                    cancel_jQ.removeClass('hide');
                } else {
                    Tip.show("添加关注失败");
                }
            }, function() {
                Tip.show("添加关注失败");
            });
        });

        //取消关注
        cancel_jQ.click(function() {
            var data = { "targetID": uid };
            Backend.unfollow(data, function(_resp) {
                if (Backend.resultValidate(_resp)) {
                    Tip.show("取消关注成功");
                    cancel_jQ.addClass('hide');
                    follow_jQ.removeClass('hide');
                } else {
                    Tip.show("取消关注失败");
                }
            }, function() {
                Tip.show("取消关注失败");
            });
        });
    };

    var nickFind = function(_page_no) {
        var data = { "nickname": nick_input_jQ.val(), "pageNo": _page_no, "pageSize": 20 };
        Backend.findUserByNickname(data, function(_resp) {
            if (Backend.pageBeanValidate(_resp)) {
                var totalRecoreds = _resp.totalRecoreds;
                result_count_jQ.html(totalRecoreds);
                result_list_jQ.empty();

                $.each(_resp.results, function(_i, _r) {
                    var one_result_jQ = $(MPT.getTmpl('one_result', _r));
                    addActionResult(one_result_jQ, _r);
                    result_list_jQ.append(one_result_jQ);
                });

                //显示分页
                var page_param = { "pageNo": _resp.pageNo, "pageSize": _resp.pageSize, "totalRecords": _resp.totalRecoreds, "totalPages": _resp.totalPages };
                var pageBar_jQ = (new Paginate).createPageBar(page_param, function(_click_page) { nickFind(_click_page); });
                page_jQ.empty().append(pageBar_jQ);

            } else {
                result_count_jQ.html(0);
                result_list_jQ.html("对不起，没有找到相关的信息");
            }
        });
        result_title_jQ.html("查找结果");
    };

    var displayInterest = function(_page_no) {

        var data = { "pageNo": _page_no, "pageSize": 20 };
        Backend.findRecommendPerson(data, function(_resp) {
            if (Backend.pageBeanValidate(_resp)) {
                var totalRecoreds = _resp.totalRecoreds;
                result_count_jQ.html(totalRecoreds);
                result_list_jQ.empty();

                $.each(_resp.results, function(_i, _r) {
                    var one_result_jQ = $(MPT.getTmpl('one_interest', $.parseJSON(_r)));
                    addActionResult(one_result_jQ, _r);
                    result_list_jQ.append(one_result_jQ);
                });

                //显示分页
                var page_param = { "pageNo": _resp.pageNo, "pageSize": _resp.pageSize, "totalRecords": _resp.totalRecoreds, "totalPages": _resp.totalPages };
                var pageBar_jQ = (new Paginate).createPageBar(page_param, function(_click_page) { displayInterest(_click_page); });
                page_jQ.empty().append(pageBar_jQ);

            } else {
                result_list_jQ.html("没有可推荐的好友");
            }
        }, function() {
            result_list_jQ.html("数据加载错误");
        });
    };

    var initialize = function() {

        Tab.switchFriend();

        //4个标签悬浮效果
        method_jQs.each(function(_i, _method_elm) {
			
			var title_jQ = $('.e_title', _method_elm);
			var detail_jQ = $('.e_detail', _method_elm);
			var arrow_jQ = $('.e_arrow', _method_elm);

            title_jQ.toggle(function() {
                title_jQ.addClass('active');
                detail_jQ.show();
            }, function() {
                title_jQ.removeClass('active');
                detail_jQ.hide();
            });
        });

        displayInterest(1);

        //社区昵称查找
        nick_submit_jQ.click(function() {
            nickFind(1);
        });
        Util.KeyBoardMonitor(nick_input_jQ, 13, nickFind);

        //获得大区信息
        SiteUtil.getServerZone(function(_areaList) {
            area_list = _areaList;
            for(var i in area_list){
                gamearea_jQ.append('<option value="'+i+'">'+area_list[i]+'</option>');
            }
        });

        //获取服信息
        SiteUtil.getServerGroup(function(_groupList) {
            server_list = _groupList;
            console.log(server_list)
            gamearea_jQ.change(function(){
                gamefwq_jQ.empty();
                for(var j in server_list){
                    var nowNub = j.split('game');
                    if(nowNub[0] == gamearea_jQ.val()){
                        gamefwq_jQ.append('<option value="'+j+'">'+server_list[j]+'</option>')
                    }
                }
            })
            gamearea_jQ.change();
        });

        //获取省份信息
        SiteUtil.getRegion(function(_province) {
            province_list = province_city_map['province'];
            for(var i in province_list){
                province_jQ.append('<option value="'+i+'">'+province_list[i]+'</option>');
            }
        });
		
		//有初始值时页面状态
		var find_url = window.location.hash.split("/");
		find_url[3];
		if(find_url[3]!=undefined){
			title_jQs.eq(0).click();
			nick_input_jQ.val(find_url[3]);
			nick_submit_jQ.click();
		}
		
    };

    initialize();
});

